﻿describe("Strategies/NakedPair", function () {

    var nakedPair;

    beforeEach(function () {
        nakedPair = new Strategies.NakedPair();
    });

    describe("pair - should find by:", function () {

        it("row", function () {

            var grid = "5-2-4-3-114-2-3---73-1-52-49235-614-467-315-2815-24-3-65-417-233-4-52-1-2-13--4--";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([8, 9]));
            var expectedContainingCells = [gridModel.cells[0][1], gridModel.cells[0][5]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[0][3], gridModel.cells[0][7]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[0][3].candidates.remove(8);
            expectedGridModel.cells[0][7].candidates.remove(8);
            expectedGridModel.cells[0][3].candidates.remove(9);
            expectedGridModel.cells[0][7].candidates.remove(9);
            expect(gridModel).toEqual(expectedGridModel);
        });

        it("column", function () {

            var grid = "1---5---7-7-914-5---3-7-1--76-581-9-9-------838-7-9-1---7--54---3-8-7-6-2---9---5";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([8, 6]));
            var expectedContainingCells = [gridModel.cells[1][0], gridModel.cells[6][0]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[2][0]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[2][0].candidates.remove(8);
            expectedGridModel.cells[2][0].candidates.remove(6);
            expect(gridModel).toEqual(expectedGridModel);
        });

        it("region", function () {

            var grid = "951-2-3862---91457----5-9214-52-9138-9-135-42132----9531--425797295138645--9--213";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([8, 6]));
            var expectedContainingCells = [gridModel.cells[1][1], gridModel.cells[2][0]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[1][2], gridModel.cells[2][1], gridModel.cells[2][2]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[1][2].candidates.remove(8);
            expectedGridModel.cells[1][2].candidates.remove(6);
            expectedGridModel.cells[2][1].candidates.remove(8);
            expectedGridModel.cells[2][1].candidates.remove(6);
            expectedGridModel.cells[2][2].candidates.remove(8);
            expectedGridModel.cells[2][2].candidates.remove(6);
            expect(gridModel).toEqual(expectedGridModel);
        });

    });

    describe("triple - should find by:", function () {

        it("row", function () {

            var grid = "--293---5-4--6--9-3----76--1--7-823--8-34--6---36----8-315----7-2--7--5-7----34--";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([2, 8, 9]));
            var expectedContainingCells = [gridModel.cells[6][4], gridModel.cells[6][6], gridModel.cells[6][7]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[6][0], gridModel.cells[6][5]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[6][0].candidates.remove(2);
            expectedGridModel.cells[6][0].candidates.remove(8);
            expectedGridModel.cells[6][0].candidates.remove(9);
            expectedGridModel.cells[6][5].candidates.remove(2);
            expectedGridModel.cells[6][5].candidates.remove(8);
            expectedGridModel.cells[6][5].candidates.remove(9);

            expect(gridModel).toEqual(expectedGridModel);
        });

        it("column", function () {

            var grid = "-2-------3-5862--9-68---32--5----------64-8-2--47--9----3-----1-----6---17-43----";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([4, 7, 9]));
            var expectedContainingCells = [gridModel.cells[0][0], gridModel.cells[2][0], gridModel.cells[4][0]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[3][0], gridModel.cells[6][0], gridModel.cells[7][0]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[3][0].candidates.remove(4);
            expectedGridModel.cells[3][0].candidates.remove(7);
            expectedGridModel.cells[3][0].candidates.remove(9);
            expectedGridModel.cells[6][0].candidates.remove(4);
            expectedGridModel.cells[6][0].candidates.remove(7);
            expectedGridModel.cells[6][0].candidates.remove(9);
            expectedGridModel.cells[7][0].candidates.remove(4);
            expectedGridModel.cells[7][0].candidates.remove(7);
            expectedGridModel.cells[7][0].candidates.remove(9);
            expect(gridModel).toEqual(expectedGridModel);
        });

        it("region", function () {

            var grid = "--------5175-8--3-9---25----1---9--669--7--5-----1-8974892-7-13-61-3----3-----4--";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([4, 6, 9]));
            var expectedContainingCells = [gridModel.cells[0][4], gridModel.cells[1][3], gridModel.cells[1][5]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[0][5], gridModel.cells[0][5], gridModel.cells[2][3]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[0][3].candidates.remove(4);
            expectedGridModel.cells[0][3].candidates.remove(6);
            expectedGridModel.cells[0][3].candidates.remove(9);
            expectedGridModel.cells[0][5].candidates.remove(4);
            expectedGridModel.cells[0][5].candidates.remove(6);
            expectedGridModel.cells[0][5].candidates.remove(9);
            expectedGridModel.cells[2][3].candidates.remove(4);
            expectedGridModel.cells[2][3].candidates.remove(6);
            expectedGridModel.cells[2][3].candidates.remove(9);
            expect(gridModel).toEqual(expectedGridModel);
        });
    });

    describe("quad - should find by:", function () {

        it("row", function () {

            var grid = "-2-4----94--189-------62-5------63--3--94-5-------8-1----------8-2--4---964-----7";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([1, 3, 5, 7]));
            var expectedContainingCells = [gridModel.cells[6][0], gridModel.cells[6][1], gridModel.cells[6][2], gridModel.cells[6][5]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[6][3], gridModel.cells[6][4], gridModel.cells[6][7], gridModel.cells[6][8]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[6][3].candidates.remove(1);
            expectedGridModel.cells[6][3].candidates.remove(3);
            expectedGridModel.cells[6][3].candidates.remove(5);
            expectedGridModel.cells[6][3].candidates.remove(7);

            expectedGridModel.cells[6][4].candidates.remove(1);
            expectedGridModel.cells[6][4].candidates.remove(3);
            expectedGridModel.cells[6][4].candidates.remove(5);
            expectedGridModel.cells[6][4].candidates.remove(7);

            expectedGridModel.cells[6][6].candidates.remove(1);
            expectedGridModel.cells[6][6].candidates.remove(3);
            expectedGridModel.cells[6][6].candidates.remove(5);
            expectedGridModel.cells[6][6].candidates.remove(7);

            expectedGridModel.cells[6][7].candidates.remove(1);
            expectedGridModel.cells[6][7].candidates.remove(3);
            expectedGridModel.cells[6][7].candidates.remove(5);
            expectedGridModel.cells[6][7].candidates.remove(7);

            expectedGridModel.cells[6][8].candidates.remove(1);
            expectedGridModel.cells[6][8].candidates.remove(3);
            expectedGridModel.cells[6][8].candidates.remove(5);
            expectedGridModel.cells[6][8].candidates.remove(7);

            expect(gridModel).toEqual(expectedGridModel);
        });

        it("column", function () {

            var grid = "5--6-4-313--7-9------5-3---469358------241396132976---6-3-9572--4--6---3----3----";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);

            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([1, 5, 7, 8]));
            var expectedContainingCells = [gridModel.cells[0][2], gridModel.cells[4][2], gridModel.cells[7][2], gridModel.cells[8][2]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[1][2], gridModel.cells[2][2]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[1][2].candidates.remove(1);
            expectedGridModel.cells[1][2].candidates.remove(5);
            expectedGridModel.cells[1][2].candidates.remove(7);
            expectedGridModel.cells[1][2].candidates.remove(8);

            expectedGridModel.cells[2][2].candidates.remove(1);
            expectedGridModel.cells[2][2].candidates.remove(5);
            expectedGridModel.cells[2][2].candidates.remove(7);
            expectedGridModel.cells[2][2].candidates.remove(8);

            expect(gridModel).toEqual(expectedGridModel);
        });

        it("region", function () {

            var grid = "----3--86----2--4--9--7852-3718562949--1423754--3976182--7-3859-392-54677--9-4132";
            var gridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(gridModel);

            //do
            var performResult = nakedPair.perform(gridModel);
           
            //expect result
            expect(performResult).toBeTruthy();
            expect(performResult.candidates).toEqual(jasmine.arrayContaining([1, 5, 6, 8]));
            var expectedContainingCells = [gridModel.cells[0][0], gridModel.cells[1][0], gridModel.cells[1][1], gridModel.cells[2][0]];
            expect(performResult.containingCells).toEqual(jasmine.arrayContaining(expectedContainingCells));
            var expectedTargetCells = [gridModel.cells[0][1], gridModel.cells[0][2], gridModel.cells[1][2], gridModel.cells[2][2]];
            expect(performResult.targetCells).toEqual(jasmine.arrayContaining(expectedTargetCells));

            var expectedGridModel = GridModel.fromShortString(grid);
            SolvingHelper.fillCandidates(expectedGridModel);
            expectedGridModel.cells[0][1].candidates.remove(1);
            expectedGridModel.cells[0][1].candidates.remove(5);
            expectedGridModel.cells[0][1].candidates.remove(6);
            expectedGridModel.cells[0][1].candidates.remove(8);

            expectedGridModel.cells[0][2].candidates.remove(1);
            expectedGridModel.cells[0][2].candidates.remove(5);
            expectedGridModel.cells[0][2].candidates.remove(6);
            expectedGridModel.cells[0][2].candidates.remove(8);

            expectedGridModel.cells[1][2].candidates.remove(1);
            expectedGridModel.cells[1][2].candidates.remove(5);
            expectedGridModel.cells[1][2].candidates.remove(6);
            expectedGridModel.cells[1][2].candidates.remove(8);

            expectedGridModel.cells[2][2].candidates.remove(1);
            expectedGridModel.cells[2][2].candidates.remove(5);
            expectedGridModel.cells[2][2].candidates.remove(6);
            expectedGridModel.cells[2][2].candidates.remove(8);

            expect(gridModel).toEqual(expectedGridModel);
        });
    });
});

